Imports System.Data.SqlClient
Imports System.ComponentModel

Public Class cls_telepeaje_

    Implements ICloneable

#Region "Variables ..."
    Private int_idtelepeaje As Integer = 0
    Private int_idvehiculo As Integer = 0
    Private str_codigo As String = ""
    Private str_nombre As String = ""
    Private str_observaciones As String = ""
    Private str_interno As String = ""
    Private str_sitch As String = ""
    Private int_idtarea As Integer = 0
    Private int_idregistro As Integer = 0
    Private dte_fechacontrato As DateTime = New System.DateTime(Now.Year, 1, 1)
    Private int_estado As enu_estado = enu_estado.activo
    Private dte_fechaestado As DateTime = New System.DateTime(Now.Year, 1, 1)
    Private str_motivoestado As String = ""


    Private obj_registro As cls_registro_ = Nothing
    Private obj_vehiculo As cls_vehiculo_ = Nothing

    Private col_Items As Collections.Generic.List(Of cls_telepeaje_) = Nothing

    Public Overrides Function ToString() As String
        Return "Sitch: " & sitch
    End Function

    Public Enum enu_estado As Integer
        <Description("Activo")> activo = 0
        <Description("Desactivo")> desactivo = 1
    End Enum

#End Region

#Region "Propiedades ..."
    Property idtelepeaje() As Integer
        Get
            idtelepeaje = int_idtelepeaje
        End Get
        Set(ByVal value As Integer)
            int_idtelepeaje = value
        End Set
    End Property
    Property idvehiculo() As Integer
        Get
            idvehiculo = int_idvehiculo
        End Get
        Set(ByVal value As Integer)
            int_idvehiculo = value
        End Set
    End Property
    Property codigo() As String
        Get
            codigo = str_codigo
        End Get
        Set(ByVal value As String)
            str_codigo = value
        End Set
    End Property
    Property nombre() As String
        Get
            nombre = str_nombre
        End Get
        Set(ByVal value As String)
            str_nombre = value
        End Set
    End Property
    Property observaciones() As String
        Get
            observaciones = str_observaciones
        End Get
        Set(ByVal value As String)
            str_observaciones = value
        End Set
    End Property
    Property interno() As String
        Get
            interno = str_interno
        End Get
        Set(ByVal value As String)
            str_interno = value
        End Set
    End Property
    Property sitch() As String
        Get
            sitch = str_sitch
        End Get
        Set(ByVal value As String)
            str_sitch = value
        End Set
    End Property
    Property idtarea() As Integer
        Get
            idtarea = int_idtarea
        End Get
        Set(ByVal value As Integer)
            int_idtarea = value
        End Set
    End Property
    Property idregistro() As Integer
        Get
            idregistro = int_idregistro
        End Get
        Set(ByVal value As Integer)
            int_idregistro = value
        End Set
    End Property
    Property fechacontrato As DateTime
        Get
            fechacontrato = dte_fechacontrato
        End Get
        Set(ByVal value As DateTime)
            dte_fechacontrato = value
        End Set
    End Property
    Property estado As enu_estado
        Get
            estado = int_estado
        End Get
        Set(ByVal value As enu_estado)
            int_estado = value
        End Set
    End Property
    Property fechaestado As DateTime
        Get
            fechaestado = dte_fechaestado
        End Get
        Set(ByVal value As DateTime)
            dte_fechaestado = value
        End Set
    End Property
    Property motivoestado As String
        Get
            motivoestado = str_motivoestado
        End Get
        Set(ByVal value As String)
            str_motivoestado = value
        End Set
    End Property

    Property registro() As cls_registro_
        Get
            If IsNothing(obj_registro) Then
                obj_registro = New cls_registro_
                If Not obj_registro.Buscar(idregistro) Then
                    obj_registro = Nothing
                End If
            End If
            registro = obj_registro
        End Get
        Set(ByVal value As cls_registro_)
            obj_registro = value
        End Set
    End Property

    Property vehiculo() As cls_vehiculo_
        Get
            If IsNothing(obj_vehiculo) Then
                obj_vehiculo = New cls_vehiculo_
                If Not obj_vehiculo.Buscar(idvehiculo) Then
                    obj_vehiculo = Nothing
                End If
            End If
            vehiculo = obj_vehiculo
        End Get
        Set(ByVal value As cls_vehiculo_)
            obj_vehiculo = value
        End Set
    End Property
    Property Items() As Collections.Generic.List(Of cls_telepeaje_)
        Get
            Items = col_Items
        End Get
        Set(ByVal value As Collections.Generic.List(Of cls_telepeaje_))
            col_Items = value
        End Set
    End Property
#End Region

    Private Sub CargarColeccion(ByVal obj_Reader As SqlDataReader)
        col_Items = New Collections.Generic.List(Of cls_telepeaje_)
        While obj_Reader.Read()
            Dim Elemento As New cls_telepeaje_
            Elemento.int_idtelepeaje = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idtelepeaje")))

            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idvehiculo"))) = False Then
                Elemento.int_idvehiculo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idvehiculo")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("codigo"))) = False Then
                Elemento.str_codigo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("codigo")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("nombre"))) = False Then
                Elemento.str_nombre = (obj_Reader.GetValue(obj_Reader.GetOrdinal("nombre")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("observaciones"))) = False Then
                Elemento.str_observaciones = (obj_Reader.GetValue(obj_Reader.GetOrdinal("observaciones")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("interno"))) = False Then
                Elemento.str_interno = (obj_Reader.GetValue(obj_Reader.GetOrdinal("interno")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("sitch"))) = False Then
                Elemento.str_sitch = (obj_Reader.GetValue(obj_Reader.GetOrdinal("sitch")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idtarea"))) = False Then
                Elemento.int_idtarea = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idtarea")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idregistro"))) = False Then
                Elemento.int_idregistro = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idregistro")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("fechacontrato"))) = False Then
                Elemento.dte_fechacontrato = (obj_Reader.GetValue(obj_Reader.GetOrdinal("fechacontrato")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("estado"))) = False Then
                Elemento.int_estado = (obj_Reader.GetValue(obj_Reader.GetOrdinal("estado")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("fechaestado"))) = False Then
                Elemento.dte_fechaestado = (obj_Reader.GetValue(obj_Reader.GetOrdinal("fechaestado")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("motivoestado"))) = False Then
                Elemento.str_motivoestado = (obj_Reader.GetValue(obj_Reader.GetOrdinal("motivoestado")))
            End If

            col_Items.Add(Elemento)
        End While
    End Sub

    Private Sub CargarVariables(ByVal obj_Reader As SqlDataReader)
        obj_Reader.Read()
        int_idtelepeaje = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idtelepeaje")))

        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idvehiculo"))) = False Then
            int_idvehiculo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idvehiculo")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("codigo"))) = False Then
            str_codigo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("codigo")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("nombre"))) = False Then
            str_nombre = (obj_Reader.GetValue(obj_Reader.GetOrdinal("nombre")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("observaciones"))) = False Then
            str_observaciones = (obj_Reader.GetValue(obj_Reader.GetOrdinal("observaciones")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("interno"))) = False Then
            str_interno = (obj_Reader.GetValue(obj_Reader.GetOrdinal("interno")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("sitch"))) = False Then
            str_sitch = (obj_Reader.GetValue(obj_Reader.GetOrdinal("sitch")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idtarea"))) = False Then
            int_idtarea = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idtarea")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idregistro"))) = False Then
            int_idregistro = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idregistro")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("fechacontrato"))) = False Then
            dte_fechacontrato = (obj_Reader.GetValue(obj_Reader.GetOrdinal("fechacontrato")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("estado"))) = False Then
            int_estado = (obj_Reader.GetValue(obj_Reader.GetOrdinal("estado")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("fechaestado"))) = False Then
            dte_fechaestado = (obj_Reader.GetValue(obj_Reader.GetOrdinal("fechaestado")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("motivoestado"))) = False Then
            str_motivoestado = (obj_Reader.GetValue(obj_Reader.GetOrdinal("motivoestado")))
        End If
    End Sub

    Public Sub Nuevo(Optional ByVal ObtenerID As Boolean = False)
        Dim obj_Conexion As New cls_Conexion
        Try
            'Creacion de registro----------------------------------------------------
            If IsNothing(obj_registro) Then
                registro = New cls_registro_
                registro.fechacreado = Now
                registro.idusuariocreado = UsuarioLOG.idusuario
                registro.fechamodificado = Now
                registro.idusuariomodificado = UsuarioLOG.idusuario
                registro.idregistroestado = 1
                registro.idusuarioregistroestado = UsuarioLOG.idusuario
                registro.Nuevo(True)
                idregistro = obj_registro.idregistro
            End If
            '------------------------------------------------------------------------

            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "telepeaje__Nuevo"
                .Parameters.Add("@idvehiculo", SqlDbType.Int).Value = int_idvehiculo
                .Parameters.Add("@codigo", SqlDbType.NVarChar).Value = str_codigo
                .Parameters.Add("@nombre", SqlDbType.NVarChar).Value = str_nombre
                .Parameters.Add("@observaciones", SqlDbType.NVarChar).Value = str_observaciones
                .Parameters.Add("@interno", SqlDbType.NVarChar).Value = str_interno
                .Parameters.Add("@sitch", SqlDbType.NVarChar).Value = str_sitch
                .Parameters.Add("@idtarea", SqlDbType.Int).Value = int_idtarea
                .Parameters.Add("@idregistro", SqlDbType.Int).Value = int_idregistro
                .Parameters.Add("@fechacontrato", SqlDbType.DateTime).Value = dte_fechacontrato
                .Parameters.Add("@estado", SqlDbType.Int).Value = int_estado
                .Parameters.Add("@fechaestado", SqlDbType.DateTime).Value = dte_fechaestado
                .Parameters.Add("@motivoestado", SqlDbType.NVarChar).Value = str_motivoestado
                .ExecuteNonQuery()
            End With
            If ObtenerID Then
                CargarUltimo()
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Crear el registro de telepeaje_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Sub Modificar()
        Dim obj_Conexion As New cls_Conexion
        Try
            'Modificacion de registro------------------------------------------------
            registro.fechamodificado = Now : registro.idusuariomodificado = UsuarioLOG.idusuario
            registro.Modificar()
            '------------------------------------------------------------------------

            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "telepeaje__Modificar"
                .Parameters.Add("@idtelepeaje", SqlDbType.Int).Value = int_idtelepeaje
                .Parameters.Add("@idvehiculo", SqlDbType.Int).Value = int_idvehiculo
                .Parameters.Add("@codigo", SqlDbType.NVarChar).Value = str_codigo
                .Parameters.Add("@nombre", SqlDbType.NVarChar).Value = str_nombre
                .Parameters.Add("@observaciones", SqlDbType.NVarChar).Value = str_observaciones
                .Parameters.Add("@interno", SqlDbType.NVarChar).Value = str_interno
                .Parameters.Add("@sitch", SqlDbType.NVarChar).Value = str_sitch
                .Parameters.Add("@idtarea", SqlDbType.Int).Value = int_idtarea
                .Parameters.Add("@idregistro", SqlDbType.Int).Value = int_idregistro
                .Parameters.Add("@fechacontrato", SqlDbType.DateTime).Value = dte_fechacontrato
                .Parameters.Add("@estado", SqlDbType.Int).Value = int_estado
                .Parameters.Add("@fechaestado", SqlDbType.DateTime).Value = dte_fechaestado
                .Parameters.Add("@motivoestado", SqlDbType.NVarChar).Value = str_motivoestado
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Modificar el registro de telepeaje_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Sub Eliminar(ByVal idtelepeaje As Integer)
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "telepeaje__Eliminar"
                .Parameters.Add("@idtelepeaje", SqlDbType.Int).Value = idtelepeaje
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Eliminar el registro de telepeaje_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Function Buscar(ByVal idtelepeaje As Integer) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            Buscar = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "telepeaje__Buscar"
                .Parameters.Add("@idtelepeaje", SqlDbType.Int).Value = idtelepeaje
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
                Buscar = True
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Buscar el registro de telepeaje_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Sub CargarUltimo()
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "telepeaje__CargarUltimo"
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al CargarUltimo registro de telepeaje_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Sub

    Public Function CargarTodos(Optional ByVal idregistroestado As Integer = 1) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            CargarTodos = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "telepeaje__CargarTodos"
                .Parameters.Add("@idregistroestado", SqlDbType.Int).Value = idregistroestado
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarColeccion(obj_Reader)
                CargarTodos = True
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al CargarTodos los registros de telepeaje_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Function CargarXidvehiculo(ByVal prm_idvehiculo As Integer) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            CargarXidvehiculo = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "telepeaje__CargarXidvehiculo"
                .Parameters.Add("@idvehiculo", SqlDbType.Int).Value = prm_idvehiculo
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarColeccion(obj_Reader)
                CargarXidvehiculo = True
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al CargarXidvehiculo los registros de telepeaje_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Function Clone() As Object Implements System.ICloneable.Clone
        Dim obj_telepeaje_Clon As New cls_telepeaje_
        obj_telepeaje_Clon.int_idtelepeaje = Me.int_idtelepeaje
        obj_telepeaje_Clon.int_idvehiculo = Me.int_idvehiculo
        obj_telepeaje_Clon.str_codigo = Me.str_codigo
        obj_telepeaje_Clon.str_nombre = Me.str_nombre
        obj_telepeaje_Clon.str_observaciones = Me.str_observaciones
        obj_telepeaje_Clon.str_interno = Me.str_interno
        obj_telepeaje_Clon.str_sitch = Me.str_sitch
        obj_telepeaje_Clon.int_idtarea = Me.int_idtarea
        obj_telepeaje_Clon.int_idregistro = Me.int_idregistro
        obj_telepeaje_Clon.col_Items = Me.col_Items
        Return obj_telepeaje_Clon
    End Function

End Class
